package com.tiger.bodyselfcheck;

import net.youmi.android.AdListener;
import net.youmi.android.AdManager;
import net.youmi.android.AdView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity implements AdListener {
	static {
		// 应用Id 应用密码 广告请求间隔(s) 测试模式 应用版本号
		AdManager.init("fb17728f5d2b3519", "8b93cc3e7be96690", 31, false, 2.1);
	}
	private int sexInt = -1;
	private EditText shengao;
	private RadioGroup sex;
	private Button lookresult, redo;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		setAdView();
		shengao = (EditText) this.findViewById(R.id.shengao);
		sex = (RadioGroup) this.findViewById(R.id.radioGroup);
		sex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int gb_id) {
				// TODO Auto-generated method stub
				if (arg0.getCheckedRadioButtonId() == R.id.RadioButton02) {
					sexInt = 0;
				} else
					sexInt = 1;
			}
		});
		lookresult = (Button) this.findViewById(R.id.lookresult);
		lookresult.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (sexInt == -1) {
					Toast.makeText(MainActivity.this, "请选择性别",
							Toast.LENGTH_LONG).show();
					return;
				}
				// TODO Auto-generated method stub
				if (shengao != null && shengao.getText().toString() != null
						&& shengao.getText().toString().length() != 0) {
					Bundle bundle = new Bundle();
					bundle.putString("shengao", shengao.getText().toString());
					Log.i("MMALL", "sex  send:" + sexInt);
					bundle.putInt("sex", sexInt);
					Intent intent = new Intent(MainActivity.this,
							ResultActivity.class);
					intent.putExtras(bundle);
					MainActivity.this.startActivity(intent);
				} else {
					Toast.makeText(MainActivity.this, "请输入身高",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		redo = (Button) this.findViewById(R.id.redo);
		redo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				shengao.setText("");
			}
		});
	}

	void setAdView() {
		// 初始化广告视图
		AdView adView = new AdView(this, Color.GRAY, Color.WHITE, 100);

		// 设置监听者(可以不设置，对获取广告没有影响)
		adView.setAdListener(this);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);

		// 设置广告出现的位置(悬浮于底部)
		params.bottomMargin = 0;
		params.gravity = Gravity.BOTTOM;

		// 将广告视图加入Activity中
		addContentView(adView, params);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 1:
			confirm(this.getString(R.string.alert_str_exit_msg), this
					.getString(R.string.menu_str_exit));
			break;
		case 2:
			AboutUS();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void AboutUS() {
		// 关于
		String about_appname = this.getResources().getString(R.string.app_name);
		String about_ver = this.getResources().getString(R.string.app_ver);
		String about_csmail = this.getResources().getString(
				R.string.about_csmail);
		String about_copyright = this.getResources().getString(
				R.string.about_copyright);
		String app_platform = this.getResources().getString(
				R.string.app_platform);

		alert(this, about_appname + "(" + app_platform + ")" + "\n版本:"
				+ about_ver + "\n客服邮箱:" + about_csmail
				+ "\nQQ:359088785\n版权信息:" + about_copyright + "", "关于");
	}

	public void confirm(String msg, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg);
		builder.setTitle(title);
		builder.setPositiveButton(R.string.alert_str_sure,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// ActivityManager
						// activityMgr=(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
						// activityMgr.restartPackage(getPackageName());
						Intent startMain = new Intent(Intent.ACTION_MAIN);
						startMain.addCategory(Intent.CATEGORY_HOME);
						startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(startMain);
						System.exit(0);
					}
				});
		builder.setNegativeButton(R.string.alert_str_cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.dismiss();
					}
				});
		builder.show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			confirm(this.getString(R.string.alert_str_exit_msg), this
					.getString(R.string.menu_str_exit));
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(1, 1, 1, this.getString(R.string.menu_str_exit)).setIcon(
				R.drawable.menu_quit);
		menu.add(1, 2, 2, this.getString(R.string.menu_str_about)).setIcon(
				R.drawable.menu_about);
		return super.onCreateOptionsMenu(menu);
	}

	public void alert(Activity act, String msg, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(act);
		builder.setMessage(msg);
		if (title != null)
			builder.setTitle(title);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {

			}
		});
		builder.show();
	}

	@Override
	public void onConnectFailed(AdView arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReceiveAd(AdView arg0) {
		// TODO Auto-generated method stub

	}
}